/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.swing.JFileChooser;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.common.BackendException;
import nu.zoom.swing.desktop.common.action.WorkbenchMessageAction;
import nu.zoom.swing.desktop.plugin.portunis.ApplicationPreferences;
import nu.zoom.swing.desktop.plugin.portunis.KeyStoreType;

import org.ops4j.gaderian.Messages;

/**
 * An action to load a keystore from disk.
 * 
 * @author $Author: johan $
 * @version $Revision: 1.1 $
 */
public class LoadKeyStoreAction extends WorkbenchMessageAction
{

	private KeyStorePlugIn plugin;
	private Workbench workbench;

	public LoadKeyStoreAction(Workbench workbench, KeyStorePlugIn plugin,
			Messages messages) {
		super(messages);
		this.plugin = plugin;
		this.workbench = workbench;
		setAcceleratorKey(KeyEvent.VK_O, KeyEvent.CTRL_MASK);
		setMnemonicKey(KeyEvent.VK_O);
		setNameFromMessages("keystore.menu.load");
		setToolTipFromMessages("keystore.menu.load.tooltip");
		setIconFromMessages("keystore.menu.load.icon");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
    @Override
	public void actionPerformed(ActionEvent e)
	{
		final ApplicationPreferences prefs = plugin.getPreferences();
		JFileChooser chooser ;
		try {
			String startDirectory = prefs.getFileChooserStartDirectory();
			chooser = new JFileChooser(startDirectory);
		} catch (BackendException e1) {
			chooser = new JFileChooser();
		}

		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setMultiSelectionEnabled(false);
		KeyStoreFileChooserAccessory accessory = new KeyStoreFileChooserAccessory(
				messages, chooser);
		chooser.setAccessory(accessory);
		boolean error = true;
		while (error) {
			error = false;
			int retval = chooser.showOpenDialog(plugin.getWorkbench()
					.getDialogOwner());

			if (retval == JFileChooser.APPROVE_OPTION) {
				File selectedFile = chooser.getSelectedFile();
				KeyStoreType type = accessory.getType();
				char[] password = accessory.getPassword();
				if (selectedFile != null) {
					try {
						plugin.loadKeystore(selectedFile, type, password);
						try {
							prefs.setFileChooserStartDirectory(selectedFile
									.getParentFile().getAbsolutePath());
						} catch (BackendException e2) {
						}
					} catch (KeyStoreException exc) {
						String message = messages
								.getMessage("keystore.load.unknown");
						workbench.getErrorReporter().reportError(message, exc);
						error = true;
					} catch (NoSuchAlgorithmException exc) {
						String message = messages
								.getMessage("keystore.load.unknown");
						workbench.getErrorReporter().reportError(message, exc);
						error = true;
					} catch (CertificateException exc) {
						String message = messages
								.getMessage("keystore.load.unknown");
						workbench.getErrorReporter().reportError(message, exc);
						error = true;
					} catch (FileNotFoundException exc) {
						String message = messages
								.getMessage("keystore.load.unknown");
						workbench.getErrorReporter().reportError(message, exc);
						error = true;
					} catch (IOException exc) {
						String message = messages
								.getMessage("keystore.load.unknown");
						workbench.getErrorReporter().reportError(message, exc);
						error = true;
					}
				} else {
					// no file selected (shouldn't happen)
				}
			} else {
				// Cancel from file chooser
			}
		}
	}
}
